package com.medicine.controller;

import com.medicine.common.Result;
import com.medicine.exception.BusinessException;
import com.medicine.model.CombatHouse;
import com.medicine.model.RoomDTO;
import com.medicine.service.ICombatHouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 输赢分数记录
 * Created by Guyver.com on 2017/10/27 14:37.
 */
@Controller
@RequestMapping("/api/game/combathouse")
public class CombatHouseController {
    @Autowired
    private ICombatHouseService combatHouseService;
    private Logger logger = LoggerFactory.getLogger(CombatHouseController.class);

    @ResponseBody
    @RequestMapping(value="/add", method= RequestMethod.POST,produces={"application/json"})
    public Result addPlayerCombatHouse(@RequestBody CombatHouse combatHouse) {
        try {
            //CombatHouse combatHouse=new CombatHouse();
            //combatHouse.setHid(hid);
            //combatHouse.setA_userid(Integer.parseInt(uid));
            this.combatHouseService.addPlayerCombatHouse(combatHouse);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "新增成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/updateOld", method= RequestMethod.POST)
    public Result updatePlayerCombatHouse(@RequestBody CombatHouse combatHouse) {
        try {

            this.combatHouseService.updatePlayerCombatHouse(combatHouse);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "更新成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/update", method= RequestMethod.POST)
    public Result updateForRoomDTO(@RequestBody RoomDTO roomDTO) {
        try {
            if("1".equals(roomDTO.getOpt())){
                List<CombatHouse> combatHouseList =this.combatHouseService.getPlayerCombatHouse(roomDTO.getRoomId());
                CombatHouse combatHouse=combatHouseList.size()>0?combatHouseList.get(0):new CombatHouse();
                for(int i=0;i<combatHouse.getB_userid().split(",").length;i++){
                    String userid=combatHouse.getB_userid().split(",")[i];
                    if(roomDTO.getUserId()!=null&&roomDTO.getUserId().equals(userid))
                    {
                        return new Result(null, "该用户已在该房间！", 0);
                    }
                }
                //判断是否满员
                if(combatHouse.getB_userid()!=null&&combatHouse.getB_userid().split(",").length>1)
                {
                    return new Result(null, "满员！", 3);
                }
                if(combatHouse.getB_userid().length()>0){
                    combatHouse.setB_userid(combatHouse.getB_userid()+","+roomDTO.getUserId());
                }else{
                    combatHouse.setB_userid(roomDTO.getUserId());
                }
                this.combatHouseService.updatePlayerCombatHouse(combatHouse);
            }else if("2".equals(roomDTO.getOpt())){
                CombatHouse combatHouse =new CombatHouse();
                combatHouse.setHid(roomDTO.getRoomId());
                combatHouse.setB_userid("");
                this.combatHouseService.updatePlayerCombatHouse(combatHouse);
            }

        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "更新成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/exithouse", method= RequestMethod.POST)
    public Result exitHouse(@RequestBody CombatHouse combatHouse) {
        try {
            if(combatHouse.getIs_del()==null){
                combatHouse.setIs_del(0);
            }
            this.combatHouseService.updatePlayerCombatHouse(combatHouse);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
        return new Result(null, "更新成功！", 0);
    }

    @ResponseBody
    @RequestMapping(value="/query", method= RequestMethod.GET)
    public Result getPlayerCombatHouse(@RequestParam String hid) {
        try {
            List<CombatHouse> combatHousesList = this.combatHouseService.getPlayerCombatHouse(hid);
            return new Result(combatHousesList, "查询成功！", 0);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
    }

    @ResponseBody
    @RequestMapping(value="/selectAllByLimit", method= RequestMethod.GET)
    public Result selectAllByLimit(@RequestParam Integer start,Integer count) {
        try {
            Map<String,Integer> map=new HashMap<>();
            map.put("start",start);
            map.put("count",count);
            List<CombatHouse> combatHousesList = this.combatHouseService.selectAllByLimit(map);
            return new Result(combatHousesList, "查询成功！", 0);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
    }

    @ResponseBody
    @RequestMapping(value="/selectAll", method= RequestMethod.GET)
    public Result selectAllByLimit() {
        try {
            List<CombatHouse> combatHousesList = this.combatHouseService.selectAll(null);

            //按照最新接口组装数据
            List list=new ArrayList();
            for(int i=0;i<combatHousesList.size();i++){
                Map oneMap=new HashMap();
                oneMap.put("roomId",combatHousesList.get(i).getHid());
                String usersString=combatHousesList.get(i).getB_userid();
                oneMap.put("users",(usersString==null||"".equals(usersString))?new ArrayList<>():usersString.split(","));
                list.add(oneMap);
            }

            return new Result(list, "查询成功！", 0);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
    }

    @ResponseBody
    @RequestMapping(value="/querybyuserid", method= RequestMethod.GET)
    public Result getPlayerCombatHouseByUserId(@RequestParam String uid) {
        try {
            List<CombatHouse> combatHousesList = this.combatHouseService.getPlayerCombatHouseByUserId(uid);
            return new Result(combatHousesList, "查询成功！", 0);
        }
        catch (BusinessException e) {
            logger.error(e.getMessage(), e);
            return new Result(null, e.getMessage(), 1);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new Result(null, "系统忙，请稍后再试！", 2);
        }
    }


}
